package com.mebs.model.service.procurement;

import com.mebs.model.service.Service;
import com.mebs.model.Country;
import com.mebs.model.CurrencyName;

import javax.persistence.*;
import java.util.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;

import org.jt.jet.annotation.Field;
import org.jt.jet.annotation.ArgumentType;

/**
 * Created by Sergey Prasolov, sap@jensentechnologies.com Date: Mar 14, 2006, 2:52:31 PM
 */
@Entity()
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "PROCUREMENT")
public class Procurement extends Service {

    @ManyToOne(optional = false)
    @JoinColumn(name = "CountryId")
    private Country country;

    @OneToOne(mappedBy = "procurement")
    @Field(type = ArgumentType.INSTANCE)
    private Commodity commodity;

    @Column(name = "StartDate", nullable = false)
    private Date startDate;

    @Column(name = "RequestedDueDate", nullable = false)
    private Date requestedDueDate;

    @Column(name = "ProcuredDate", nullable = true)
    private Date procuredDate;

    @Column(name = "DeliveredDate", nullable = true)
    private Date deliveredDate;

    @Column(name = "PurchaseComments", nullable = true, length = 1024)
    private String purchaseComments;

    @ManyToOne(optional = true)
    @JoinColumn(name = "StatusId")
    @Field(type = ArgumentType.INSTANCE)
    private ProcurementStatus status;

    @Column(name = "AdditionalCharges", nullable = true)
    private Double additionalCharges;

    @ManyToOne(optional = true)
    @JoinColumn(name = "CurrencyNameId")
    @Field(type = ArgumentType.INSTANCE)
    private CurrencyName currencyName;


    /**
     * Getters-setters
     */

    public Country getCountry()
    {
        return country;
    }

    public void setCountry(Country country)
    {
        this.country = country;
    }

    public Commodity getCommodity()
    {
        return commodity;
    }

    public void setCommodity(Commodity commodity)
    {
        this.commodity = commodity;
    }

    public Date getStartDate()
    {
        return startDate;
    }

    public void setStartDate(Date startDate)
    {
        this.startDate = startDate;
    }

    public Date getRequestedDueDate()
    {
        return requestedDueDate;
    }

    public void setRequestedDueDate(Date requestedDueDate)
    {
        this.requestedDueDate = requestedDueDate;
    }

    public Date getProcuredDate()
    {
        return procuredDate;
    }

    public void setProcuredDate(Date procuredDate)
    {
        this.procuredDate = procuredDate;
    }

    public Date getDeliveredDate()
    {
        return deliveredDate;
    }

    public void setDeliveredDate(Date deliveredDate)
    {
        this.deliveredDate = deliveredDate;
    }

    public String getPurchaseComments()
    {
        return purchaseComments;
    }

    public void setPurchaseComments(String purchaseComments)
    {
        this.purchaseComments = purchaseComments;
    }

    public ProcurementStatus getStatus()
    {
        return status;
    }

    public void setStatus(ProcurementStatus status)
    {
        this.status = status;
    }

    public Double getAdditionalCharges()
    {
        return additionalCharges;
    }

    public void setAdditionalCharges(Double additionalCharges)
    {
        this.additionalCharges = additionalCharges;
    }

    public CurrencyName getCurrencyName()
    {
        return currencyName;
    }

    public void setCurrencyName(CurrencyName currencyName)
    {
        this.currencyName = currencyName;
    }

    @Transient
    public Type getRequirementType()
    {
        return Type.PROCUREMENT;
    }

    @Transient
    public boolean isDeletable()
    {
        return commodity.isDeletable();
    }

    @Override
    public String toString()
    {
        return (this.getSequence() != null) ? this.getSequence().toString() : "";
    }

    @Transient
    public String getSequenceNumber()
    {
        Calendar c = new GregorianCalendar();
        c.setTime(getStartDate());
        return String.format("PRO-%s-%05d-%02d%02d%02d",
            getCompany().getTicker() == null ? "XXX" : getCompany().getTicker(),
            getSequence(),
            c.get(Calendar.YEAR) % 100,
            c.get(Calendar.MONTH) + 1,
            c.get(Calendar.DAY_OF_MONTH));
    }
}
